import Vue from 'vue'
import VueRouter from 'vue-router'
//导入登录组件
// import login from '../components/login.vue'
// import home from '../components/home.vue'
// import welcome from '../components/welcome.vue'
// import user from '../components/user/user.vue'
// import roles from '../components/rights/roles.vue'
// import rights from '../components/rights/rights.vue'
// import goods from '../components/goods/goods.vue'
// import params from '../components/goods/params.vue'
// import categories from '../components/goods/categories.vue'
// import orders from '../components/order/orders.vue'
// import reports from '../components/reports/reports.vue'
// import addgoods from '../components/goods/addgoods.vue'

// 导入登录组件
const login = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '@/views/login.vue')
const home = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '@/views/home.vue')
const welcome = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '@/views/welcome.vue')

// 这个 @ 符号，是 vue 给提供的一个路径简写方式，代表的是 src 目录
// 注意：@ 后面有一个斜线的
const user = () =>
    import ( /* webpackChunkName: "users" */ '@/views/user/user.vue')

const roles = () =>
    import ( /* webpackChunkName: "rights_roles" */ '@/views/rights/roles.vue')
const rights = () =>
    import ( /* webpackChunkName: "rights_roles" */ '@/views/rights/rights.vue')

const goods = () =>
    import ( /* webpackChunkName: "goods" */ '@/views/goods/goods.vue')
const params = () =>
    import ( /* webpackChunkName: "goods" */ '@/views/goods/params.vue')
const categories = () =>
    import ( /* webpackChunkName: "goods" */ '@/views/goods/categories.vue')
const orders = () =>
    import ( /* webpackChunkName: "goods" */ '@/views/order/orders.vue')

const reports = () =>
    import ( /* webpackChunkName: "order" */ '@/views/reports/reports.vue')

const addgoods = () =>
    import ( /* webpackChunkName: "reports" */ '@/views/goods/addgoods.vue')

Vue.use(VueRouter)
    // 创建路由规则
const routes = [
        { path: '/', redirect: '/login' },
        { path: '/login', component: login },
        {
            path: '/home',
            component: home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: welcome },
                { path: '/users', component: user },
                { path: '/roles', component: roles },
                { path: '/rights', component: rights },
                { path: '/goods', component: goods },
                { path: '/params', component: params },
                { path: '/categories', component: categories },
                { path: '/orders', component: orders },
                { path: '/reports', component: reports },
                { path: '/goods/addgoods', component: addgoods }
            ]
        }
    ]
    // 挂载路由
const router = new VueRouter({
        routes
    })
    //路由导航守卫 验证用户是否有token值 如果没有强制跳转到登录页
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const token = sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
})
export default router